Skip to contentSkip to conversation

Change in openvpn[master]: Add support for HAProxy's PROXY protocol

Openvpn-devel ralf_lici (Code Review) 1 participant 10 messages Jul 26, 2024 – Jan 9, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1721982049000.I9766c68feaba71279f26c3310eee4b5ca215e6ac@...2715...

#1 lore raw

Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/685?usp=email

to review the following change.

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Add support for HAProxy's PROXY protocol

The PROXY protocol is a simple protocol that can be used to communicate connection information such as a client's address across multiple layers of NAT or TCP proxies. This commit adds support for the parsing of the v1 and v2 PROXY protocol header but does not yet parse the TLV data in the v2 header.

Check if a PROXY protocol header was sent by the client at the beginning of the connection. If so, parse it, store the extracted informations and replace the proxy address with the real client address. Checking for the header is done only if the packet has an invalid length so that there's minimal overhead for the common case.

Also add a small document file showing how to configure HAProxy to test the feature.

Change-Id I9766c68feaba71279f26c3310eee4b5ca215e6ac

---
M CMakeLists.txt
A doc/proxy-protocol.txt
M src/openvpn/Makefile.am
M src/openvpn/init.c
M src/openvpn/multi.c
M src/openvpn/openvpn.h
A src/openvpn/proxy_protocol.c
A src/openvpn/proxy_protocol.h
M src/openvpn/socket.c

9 files changed, 895 insertions(+), 7 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/1

CMakeLists.txt +2 -0
index ad620fa..67cfd08 100644
@@ -490,6 +490,8 @@
     src/openvpn/proto.h
     src/openvpn/proxy.c
     src/openvpn/proxy.h
+    src/openvpn/proxy_protocol.c
+    src/openvpn/proxy_protocol.h
     src/openvpn/ps.c
     src/openvpn/ps.h
     src/openvpn/push.c
doc/proxy-protocol.txt new file +157 -0

157 lines not on this page · raw message

src/openvpn/Makefile.am +1 -0
index 56cce9d..fc037ca 100644
@@ -111,6 +111,7 @@
 	pool.c pool.h \
 	proto.c proto.h \
 	proxy.c proxy.h \
+	proxy_protocol.c proxy_protocol.h \
 	ps.c ps.h \
 	push.c push.h \
 	pushlist.h \
src/openvpn/init.c +7 -1
index a49e563..cbce217 100644
@@ -54,6 +54,7 @@
 #include "mss.h"
 #include "mudp.h"
 #include "dco.h"
+#include "proxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -680,6 +681,11 @@
         c->c1.socks_proxy = NULL;
         c->c1.socks_proxy_owned = false;
     }
+    if (c->c1.proxy_protocol)
+    {
+        proxy_protocol_free(c->c1.proxy_protocol);
+        c->c1.proxy_protocol = NULL;
+    }
 }
 
 static void
@@ -4774,7 +4780,7 @@
         /* free up environmental variable store */
         do_env_set_destroy(c);
 
-        /* close HTTP or SOCKS proxy */
+        /* close HTTP or SOCKS proxy or PROXY protocol object */
         uninit_proxy(c);
 
         /* garbage collect */
src/openvpn/multi.c +73 -2

112 lines not on this page · raw message

src/openvpn/openvpn.h +3 -0
index e98c93e..ded6103 100644
@@ -45,6 +45,7 @@
 #include "pool.h"
 #include "plugin.h"
 #include "manage.h"
+#include "proxy_protocol.h"
 
 /*
  * Our global key schedules, packaged thusly
@@ -191,6 +192,8 @@
     struct socks_proxy_info *socks_proxy;
     bool socks_proxy_owned;
 
+    struct proxy_protocol_info *proxy_protocol;
+
     /* persist --ifconfig-pool db to file */
     struct ifconfig_pool_persist *ifconfig_pool_persist;
     bool ifconfig_pool_persist_owned;
src/openvpn/proxy_protocol.c new file +472 -0

472 lines not on this page · raw message

src/openvpn/proxy_protocol.h new file +165 -0

165 lines not on this page · raw message

src/openvpn/socket.c +15 -4
index 17c5e76..7fb382c 100644
@@ -39,6 +39,7 @@
 #include "manage.h"
 #include "openvpn.h"
 #include "forward.h"
+#include "proxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -2680,10 +2681,20 @@
 
         if (sb->len < 1 || sb->len > sb->maxlen)
         {
-            msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
-            stream_buf_reset(sb);
-            sb->error = true;
-            return false;
+            /* check if it's a PROXY protocol header */
+
+            /* undo the reading of net_size */
+            ASSERT(buf_prepend(&sb->buf, sizeof(net_size)));
+            const proxy_protocol_version_t pp_version = proxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf));
+            const int pp_len = proxy_protocol_header_len(BPTR(&sb->buf), BLEN(&sb->buf), pp_version);
+            if (pp_version <= 0 || pp_len < 0 || pp_len > sb->buf.len)
+            {
+                msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
+                stream_buf_reset(sb);
+                sb->error = true;
+                return false;
+            }
+            sb->len = pp_len;
         }
     }
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 1
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos, ralf_lici.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 1: Code-Review-1

(3 comments)

Patchset:

PS1:
Only looked through it superficially. Aside from the comments it would be good if we could have some kind of unit-test for the parsing code.

File src/openvpn/proxy_protocol.h:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/364aa307_26397b78 :
PS1, Line 8: * Copyright (C) 2002-2024 OpenVPN Inc <[email protected]...> Wrong copyright

File src/openvpn/proxy_protocol.c:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/f678bd92_399015cd :
PS1, Line 8: * Copyright (C) 2002-2024 OpenVPN Inc <[email protected]...> Wrong copyright

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 1
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: ralf_lici <[email protected]...>
Gerrit-Comment-Date: Fri, 26 Jul 2024 09:42:44 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: ralf_lici.

plaisthos has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 1: Code-Review-1

(9 comments)

Patchset:

PS1:
I think this needs more overhaul

File CMakeLists.txt:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/59d24823_1499669a :
PS1, Line 494: src/openvpn/proxy_protocol.h
haproxy_protocol.c/haproxy_protol.h

Currently it looks like they are related to the other proxy code which they are not.

File doc/proxy-protocol.txt:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/4932a48c_3085af39 :
PS1, Line 8: https://www.haproxy.org/download/2.4/doc/proxy-protocol.txt
Is this just a copy and paste of the document or is this addition to that document? It would be good to clarify the relationship between these docuemnts.

http://gerrit.openvpn.net/c/openvpn/+/685/comment/4a84e43a_90eb3cad :
PS1, Line 11: a binary version (v2).
Is this protocol TCP only? I assume so, so would be good to mention this.

http://gerrit.openvpn.net/c/openvpn/+/685/comment/1508ed20_2e564dcb :
PS1, Line 26: ---------
Why are supporting two new protocls? Is there a need to introduce an already deprecated protocol to OpenVPN?

http://gerrit.openvpn.net/c/openvpn/+/685/comment/f26ed461_54c16e70 :
PS1, Line 97: The v2 signature of the protocol: "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x20".
Really? The singnature is '\r\n\r\n\x00\r\nQUIT\n'?

http://gerrit.openvpn.net/c/openvpn/+/685/comment/28038db2_443fb0fb :
PS1, Line 108:
What does this mean in the openvpn context?

http://gerrit.openvpn.net/c/openvpn/+/685/comment/aff6de14_525b6ce9 :
PS1, Line 115: DGRAM (0x2).
if this is dgram, is the protocol still over tcp or does it use UDP then?

File src/openvpn/socket.c:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/9d712c5d_d6c36d0f :
PS1, Line 2684: /* check if it's a PROXY protocol header */
I don't think this is the right place to add this. This will be basically called on every link_socket_read_tcp and not just on the first packets. Also it seems to be enabled always so this might have side effects on non-haproxy operation.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 1
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: ralf_lici <[email protected]...>
Gerrit-Comment-Date: Fri, 26 Jul 2024 11:45:33 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 1:

(11 comments)

Patchset:

PS1:

Quoted 1 line

Only looked through it superficially. […]

Sure, I'm working on it

File CMakeLists.txt:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/7290bcc7_a1b2bf59 :
PS1, Line 494: src/openvpn/proxy_protocol.h

Quoted 1 line

haproxy_protocol.c/haproxy_protol.h […]

Acknowledged

File doc/proxy-protocol.txt:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/98eb2e63_aa08eed1 :
PS1, Line 8: https://www.haproxy.org/download/2.4/doc/proxy-protocol.txt

Quoted 1 line

Is this just a copy and paste of the document or is this addition to that document? It would be good […]

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/685/comment/7d238309_2cbc8ac3 :
PS1, Line 11: a binary version (v2).

Quoted 1 line

Is this protocol TCP only? I assume so, so would be good to mention this.

It also supports UDP but we only handle the TCP case so I will state it more clearly in the document.

http://gerrit.openvpn.net/c/openvpn/+/685/comment/83291020_4f4e27ad :
PS1, Line 26: ---------

Quoted 1 line

Why are supporting two new protocls? Is there a need to introduce an already deprecated protocol to […]

Actually, as far as I have seen, currently some proxies only support v1 (eg. stunnel) so I don't think this version can be considered deprecated yet.

http://gerrit.openvpn.net/c/openvpn/+/685/comment/e62ae60e_87dd1528 :
PS1, Line 97: The v2 signature of the protocol: "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x20".

Quoted 1 line

Really? The singnature is '\r\n\r\n\x00\r\nQUIT\n'?

Yes. Citing the specification (section 6):
This means that most protocols and implementations will not be confused by an incoming connection exhibiting the protocol signature, which avoids issues when facing misconfigurations.

http://gerrit.openvpn.net/c/openvpn/+/685/comment/55c354bf_e38e4ac0 :
PS1, Line 108:

Quoted 1 line

What does this mean in the openvpn context?

Basically LOCAL commands are used when the proxy sends health-checks to the server and therefore I guess these should never arrive to an openvpn server and should be ignored. I will add this info to the document

http://gerrit.openvpn.net/c/openvpn/+/685/comment/a24cdcda_47d21b79 :
PS1, Line 115: DGRAM (0x2).

Quoted 1 line

if this is dgram, is the protocol still over tcp or does it use UDP then?

The PROXY protocol works both with TCP and with UDP but we support only TCP. I will write in the document that we support only TCP.

File src/openvpn/proxy_protocol.h:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/9bd9130b_22b62cc0 :
PS1, Line 8: * Copyright (C) 2002-2024 OpenVPN Inc <[email protected]...>

Quoted 1 line

Wrong copyright

Acknowledged

File src/openvpn/proxy_protocol.c:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/af015d00_a4fef0e9 :
PS1, Line 8: * Copyright (C) 2002-2024 OpenVPN Inc <[email protected]...>

Quoted 1 line

Wrong copyright

Acknowledged

File src/openvpn/socket.c:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/a5c360d3_bd5f87ca :
PS1, Line 2684: /* check if it's a PROXY protocol header */

Quoted 1 line

I don't think this is the right place to add this. […]

It's true that it doesn't get executed only on the first packet. However, there's a change in multi.c that checks if the header is present only once at the beginning of a TCP connection and parses it. So, even if we were to detect a proxy header later, we would not parse it. Additionally, we don't expect proxy headers after the connection has begun. Therefore, if we end up in this "if" condition, it will probably be due to a malformed packet, and the connection will be dropped.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 1
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Fri, 26 Jul 2024 13:27:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: plaisthos <[email protected]...>
Comment-In-Reply-To: flichtenheld <[email protected]...>
Gerrit-MessageType: comment

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/685?usp=email

to look at the new patch set (#2).

The following approvals got outdated and were removed:
Code-Review-1 by flichtenheld, Code-Review-1 by plaisthos

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Add support for HAProxy's PROXY protocol

The PROXY protocol is a simple protocol that can be used to communicate connection information such as a client's address across multiple layers of NAT or TCP proxies. This commit adds support for the parsing of the v1 and v2 PROXY protocol header but does not yet parse the TLV data in the v2 header.

Check if a PROXY protocol header was sent by the client at the beginning of the connection. If so, parse it, store the extracted informations and replace the proxy address with the real client address. Checking for the header is done only if the packet has an invalid length so that there's minimal overhead for the common case.

Also add a small document file showing how to configure HAProxy to test the feature and a unit test for the parsing.

Change-Id I9766c68feaba71279f26c3310eee4b5ca215e6ac

---
M CMakeLists.txt
A doc/haproxy-protocol.txt
M src/openvpn/Makefile.am
A src/openvpn/haproxy_protocol.c
A src/openvpn/haproxy_protocol.h
M src/openvpn/init.c
M src/openvpn/multi.c
M src/openvpn/openvpn.h
M src/openvpn/socket.c
M src/openvpn/socket.h
M tests/unit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_haproxy_protocol.c

12 files changed, 1,324 insertions(+), 13 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/2

CMakeLists.txt +2 -0
index ad620fa..e927a01 100644
@@ -419,6 +419,8 @@
     src/openvpn/fragment.h
     src/openvpn/gremlin.c
     src/openvpn/gremlin.h
+    src/openvpn/haproxy_protocol.c
+    src/openvpn/haproxy_protocol.h
     src/openvpn/helper.c
     src/openvpn/helper.h
     src/openvpn/httpdigest.c
doc/haproxy-protocol.txt new file +166 -0

166 lines not on this page · raw message

src/openvpn/Makefile.am +1 -0
index 56cce9d..9dce362 100644
@@ -65,6 +65,7 @@
 	forward.c forward.h \
 	fragment.c fragment.h \
 	gremlin.c gremlin.h \
+	haproxy_protocol.c haproxy_protocol.h \
 	helper.c helper.h \
 	httpdigest.c httpdigest.h \
 	lladdr.c lladdr.h \
src/openvpn/haproxy_protocol.c new file +487 -0

487 lines not on this page · raw message

src/openvpn/haproxy_protocol.h new file +185 -0

185 lines not on this page · raw message

src/openvpn/init.c +10 -1
index a49e563..ecd3253 100644
@@ -54,6 +54,7 @@
 #include "mss.h"
 #include "mudp.h"
 #include "dco.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -3926,6 +3927,14 @@
 
         c->c1.link_socket_addr.bind_local = NULL;
     }
+
+    /* Clear and free the memory allocated for the HAProxy protocol */
+    if (c->c1.haproxy_protocol)
+    {
+        haproxy_protocol_reset(c->c1.haproxy_protocol);
+        free(c->c1.haproxy_protocol);
+        c->c1.haproxy_protocol = NULL;
+    }
 }
 
 /*
@@ -4774,7 +4783,7 @@
         /* free up environmental variable store */
         do_env_set_destroy(c);
 
-        /* close HTTP or SOCKS proxy */
+        /* close HTTP or SOCKS proxy or PROXY protocol object */
         uninit_proxy(c);
 
         /* garbage collect */
src/openvpn/multi.c +74 -2

119 lines not on this page · raw message

src/openvpn/openvpn.h +3 -0
index e98c93e..a192f7a 100644
@@ -45,6 +45,7 @@
 #include "pool.h"
 #include "plugin.h"
 #include "manage.h"
+#include "haproxy_protocol.h"
 
 /*
  * Our global key schedules, packaged thusly
@@ -191,6 +192,8 @@
     struct socks_proxy_info *socks_proxy;
     bool socks_proxy_owned;
 
+    struct haproxy_protocol_info *haproxy_protocol;
+
     /* persist --ifconfig-pool db to file */
     struct ifconfig_pool_persist *ifconfig_pool_persist;
     bool ifconfig_pool_persist_owned;
src/openvpn/socket.c +26 -8
index 17c5e76..f096a20 100644
@@ -39,6 +39,7 @@
 #include "manage.h"
 #include "openvpn.h"
 #include "forward.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -1617,7 +1618,7 @@
 stream_buf_close(struct stream_buf *sb);
 
 static bool
-stream_buf_added(struct stream_buf *sb, int length_added);
+stream_buf_added(struct stream_buf *sb, int length_added, bool haproxy_protocol_allowed);
 
 /* For stream protocols, allocate a buffer to build up packet.
  * Called after frame has been finalized. */
@@ -2629,7 +2630,7 @@
     {
         ASSERT(buf_copy(&sock->stream_buf.buf, &sock->stream_buf.residual));
         ASSERT(buf_init(&sock->stream_buf.residual, 0));
-        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0);
+        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0, false);
         dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=%d",
              sock->stream_buf.residual_fully_formed ? "YES" : "NO",
              sock->stream_buf.residual.len);
@@ -2644,7 +2645,8 @@
 
 static bool
 stream_buf_added(struct stream_buf *sb,
-                 int length_added)
+                 int length_added,
+                 const bool haproxy_protocol_allowed)
 {
     dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=%d", length_added);
     if (length_added > 0)
@@ -2680,10 +2682,26 @@
 
         if (sb->len < 1 || sb->len > sb->maxlen)
         {
-            msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
-            stream_buf_reset(sb);
-            sb->error = true;
-            return false;
+            haproxy_protocol_version_t hp_version = HAPROXY_PROTOCOL_VERSION_UNKNOWN;
+            int hp_len = -1;
+            /* if it's allowed (first packet), check for a HAProxy protocol header */
+            if (haproxy_protocol_allowed)
+            {
+                /* undo the reading of net_size */
+                ASSERT(buf_prepend(&sb->buf, sizeof(net_size)));
+                hp_version = haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf));
+                hp_len = haproxy_protocol_header_len(BPTR(&sb->buf), BLEN(&sb->buf), hp_version);
+            }
+
+            /* we do not allow partial headers */
+            if (hp_version <= 0 || hp_len < 0 || hp_len > sb->buf.len)
+            {
+                msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
+                stream_buf_reset(sb);
+                sb->error = true;
+                return false;
+            }
+            sb->len = hp_len;
         }
     }
 
@@ -3267,7 +3285,7 @@
     }
 
     if (sock->stream_buf.residual_fully_formed
-        || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */
+        || stream_buf_added(&sock->stream_buf, len, sock->haproxy_protocol_allowed)) /* packet complete? */
     {
         stream_buf_get_final(&sock->stream_buf, buf);
         stream_buf_reset(&sock->stream_buf);
src/openvpn/socket.h +3 -0
index 47083ad..1132b35 100644
@@ -227,6 +227,9 @@
     const char *proxy_dest_host;
     const char *proxy_dest_port;
 
+    /* check HAProxy PROXY protocol only if true */
+    bool haproxy_protocol_allowed;
+
     /* Pointer to the server-poll to trigger the timeout in function which have
      * their own loop instead of using the main oop */
     struct event_timeout *server_poll_timeout;
tests/unit_tests/openvpn/Makefile.am +13 -2
index a4e6235..1802132 100644
@@ -10,8 +10,9 @@
 test_binaries += argv_testdriver buffer_testdriver
 endif
 
-test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
-	pkt_testdriver ssl_testdriver user_pass_testdriver
+test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver \
+				 haproxy_protocol_testdriver ncp_testdriver misc_testdriver \
+				 pkt_testdriver ssl_testdriver user_pass_testdriver
 
 if HAVE_LD_WRAP_SUPPORT
 if !WIN32
@@ -73,6 +74,16 @@
 	$(top_srcdir)/src/openvpn/win32-util.c \
 	$(top_srcdir)/src/openvpn/mss.c
 
+haproxy_protocol_testdriver_CFLAGS  = \
+	-I$(top_srcdir)/include -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/compat \
+	@TEST_CFLAGS@
+haproxy_protocol_testdriver_LDFLAGS = @TEST_LDFLAGS@
+haproxy_protocol_testdriver_SOURCES = test_haproxy_protocol.c mock_msg.c mock_msg.h \
+	mock_get_random.c \
+	$(top_srcdir)/src/openvpn/platform.c \
+	$(top_srcdir)/src/openvpn/buffer.c \
+	$(top_srcdir)/src/openvpn/haproxy_protocol.c
+
 ssl_testdriver_CFLAGS  = \
 	-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
 	@TEST_CFLAGS@
tests/unit_tests/openvpn/test_haproxy_protocol.c new file +354 -0

354 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 2
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#6 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 2:

(1 comment)

File src/openvpn/socket.c:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/52c62000_0d470c9e :
PS1, Line 2684: /* check if it's a PROXY protocol header */

Quoted 1 line

It's true that it doesn't get executed only on the first packet. However, there's a change in multi. […]

I added an argument to the function that ensures the haproxy protocol header detection is done only once at the beginning of the connection.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 2
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Wed, 31 Jul 2024 09:20:29 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: plaisthos <[email protected]...>
Comment-In-Reply-To: ralf_lici <[email protected]...>
Gerrit-MessageType: comment

#7 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/685?usp=email

to look at the new patch set (#4).

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Add support for HAProxy's PROXY protocol

The PROXY protocol is a simple protocol that can be used to communicate connection information such as a client's address across multiple layers of NAT or TCP proxies. This commit adds support for the parsing of the v1 and v2 PROXY protocol header but does not yet parse the TLV data in the v2 header.

Check if a PROXY protocol header was sent by the client at the beginning of the connection. If so, parse it, store the extracted informations and replace the proxy address with the real client address. Checking for the header is done only if the packet has an invalid length so that there's minimal overhead for the common case.

Also add a small document file showing how to configure HAProxy to test the feature and a unit test for the parsing.

Change-Id I9766c68feaba71279f26c3310eee4b5ca215e6ac

---
M CMakeLists.txt
A doc/haproxy-protocol.txt
M src/openvpn/Makefile.am
A src/openvpn/haproxy_protocol.c
A src/openvpn/haproxy_protocol.h
M src/openvpn/init.c
M src/openvpn/multi.c
M src/openvpn/openvpn.h
M src/openvpn/socket.c
M src/openvpn/socket.h
M tests/unit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_haproxy_protocol.c

12 files changed, 1,326 insertions(+), 14 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/4

CMakeLists.txt +2 -0
index ad620fa..e927a01 100644
@@ -419,6 +419,8 @@
     src/openvpn/fragment.h
     src/openvpn/gremlin.c
     src/openvpn/gremlin.h
+    src/openvpn/haproxy_protocol.c
+    src/openvpn/haproxy_protocol.h
     src/openvpn/helper.c
     src/openvpn/helper.h
     src/openvpn/httpdigest.c
doc/haproxy-protocol.txt new file +166 -0

166 lines not on this page · raw message

src/openvpn/Makefile.am +1 -0
index 56cce9d..9dce362 100644
@@ -65,6 +65,7 @@
 	forward.c forward.h \
 	fragment.c fragment.h \
 	gremlin.c gremlin.h \
+	haproxy_protocol.c haproxy_protocol.h \
 	helper.c helper.h \
 	httpdigest.c httpdigest.h \
 	lladdr.c lladdr.h \
src/openvpn/haproxy_protocol.c new file +487 -0

487 lines not on this page · raw message

src/openvpn/haproxy_protocol.h new file +184 -0

184 lines not on this page · raw message

src/openvpn/init.c +10 -1
index a49e563..ecd3253 100644
@@ -54,6 +54,7 @@
 #include "mss.h"
 #include "mudp.h"
 #include "dco.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -3926,6 +3927,14 @@
 
         c->c1.link_socket_addr.bind_local = NULL;
     }
+
+    /* Clear and free the memory allocated for the HAProxy protocol */
+    if (c->c1.haproxy_protocol)
+    {
+        haproxy_protocol_reset(c->c1.haproxy_protocol);
+        free(c->c1.haproxy_protocol);
+        c->c1.haproxy_protocol = NULL;
+    }
 }
 
 /*
@@ -4774,7 +4783,7 @@
         /* free up environmental variable store */
         do_env_set_destroy(c);
 
-        /* close HTTP or SOCKS proxy */
+        /* close HTTP or SOCKS proxy or PROXY protocol object */
         uninit_proxy(c);
 
         /* garbage collect */
src/openvpn/multi.c +74 -2

119 lines not on this page · raw message

src/openvpn/openvpn.h +3 -0
index e98c93e..a192f7a 100644
@@ -45,6 +45,7 @@
 #include "pool.h"
 #include "plugin.h"
 #include "manage.h"
+#include "haproxy_protocol.h"
 
 /*
  * Our global key schedules, packaged thusly
@@ -191,6 +192,8 @@
     struct socks_proxy_info *socks_proxy;
     bool socks_proxy_owned;
 
+    struct haproxy_protocol_info *haproxy_protocol;
+
     /* persist --ifconfig-pool db to file */
     struct ifconfig_pool_persist *ifconfig_pool_persist;
     bool ifconfig_pool_persist_owned;
src/openvpn/socket.c +29 -9
index 17c5e76..90914dd 100644
@@ -39,6 +39,7 @@
 #include "manage.h"
 #include "openvpn.h"
 #include "forward.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -1617,7 +1618,7 @@
 stream_buf_close(struct stream_buf *sb);
 
 static bool
-stream_buf_added(struct stream_buf *sb, int length_added);
+stream_buf_added(struct stream_buf *sb, int length_added, bool haproxy_protocol_allowed);
 
 /* For stream protocols, allocate a buffer to build up packet.
  * Called after frame has been finalized. */
@@ -2629,7 +2630,7 @@
     {
         ASSERT(buf_copy(&sock->stream_buf.buf, &sock->stream_buf.residual));
         ASSERT(buf_init(&sock->stream_buf.residual, 0));
-        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0);
+        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0, false);
         dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=%d",
              sock->stream_buf.residual_fully_formed ? "YES" : "NO",
              sock->stream_buf.residual.len);
@@ -2644,7 +2645,8 @@
 
 static bool
 stream_buf_added(struct stream_buf *sb,
-                 int length_added)
+                 int length_added,
+                 const bool haproxy_protocol_allowed)
 {
     dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=%d", length_added);
     if (length_added > 0)
@@ -2661,7 +2663,9 @@
 #if PORT_SHARE
         if (sb->port_share_state == PS_ENABLED)
         {
-            if (!is_openvpn_protocol(&sb->buf))
+            if (!is_openvpn_protocol(&sb->buf) &&
+                (!haproxy_protocol_allowed ||
+                haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf)) == HAPROXY_PROTOCOL_VERSION_INVALID))
             {
                 msg(D_STREAM_ERRORS, "Non-OpenVPN client protocol detected");
                 sb->port_share_state = PS_FOREIGN;
@@ -2680,10 +2684,26 @@
 
         if (sb->len < 1 || sb->len > sb->maxlen)
         {
-            msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
-            stream_buf_reset(sb);
-            sb->error = true;
-            return false;
+            haproxy_protocol_version_t hp_version = HAPROXY_PROTOCOL_VERSION_UNKNOWN;
+            int hp_len = -1;
+            /* if it's allowed (first packet), check for a HAProxy protocol header */
+            if (haproxy_protocol_allowed)
+            {
+                /* undo the reading of net_size */
+                ASSERT(buf_prepend(&sb->buf, sizeof(net_size)));
+                hp_version = haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf));
+                hp_len = haproxy_protocol_header_len(BPTR(&sb->buf), BLEN(&sb->buf), hp_version);
+            }
+
+            /* we do not allow partial headers */
+            if (hp_version <= 0 || hp_len < 0 || hp_len > sb->buf.len)
+            {
+                msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
+                stream_buf_reset(sb);
+                sb->error = true;
+                return false;
+            }
+            sb->len = hp_len;
         }
     }
 
@@ -3267,7 +3287,7 @@
     }
 
     if (sock->stream_buf.residual_fully_formed
-        || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */
+        || stream_buf_added(&sock->stream_buf, len, sock->haproxy_protocol_allowed)) /* packet complete? */
     {
         stream_buf_get_final(&sock->stream_buf, buf);
         stream_buf_reset(&sock->stream_buf);
src/openvpn/socket.h +3 -0
index 47083ad..1132b35 100644
@@ -227,6 +227,9 @@
     const char *proxy_dest_host;
     const char *proxy_dest_port;
 
+    /* check HAProxy PROXY protocol only if true */
+    bool haproxy_protocol_allowed;
+
     /* Pointer to the server-poll to trigger the timeout in function which have
      * their own loop instead of using the main oop */
     struct event_timeout *server_poll_timeout;
tests/unit_tests/openvpn/Makefile.am +13 -2
index a4e6235..1802132 100644
@@ -10,8 +10,9 @@
 test_binaries += argv_testdriver buffer_testdriver
 endif
 
-test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
-	pkt_testdriver ssl_testdriver user_pass_testdriver
+test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver \
+				 haproxy_protocol_testdriver ncp_testdriver misc_testdriver \
+				 pkt_testdriver ssl_testdriver user_pass_testdriver
 
 if HAVE_LD_WRAP_SUPPORT
 if !WIN32
@@ -73,6 +74,16 @@
 	$(top_srcdir)/src/openvpn/win32-util.c \
 	$(top_srcdir)/src/openvpn/mss.c
 
+haproxy_protocol_testdriver_CFLAGS  = \
+	-I$(top_srcdir)/include -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/compat \
+	@TEST_CFLAGS@
+haproxy_protocol_testdriver_LDFLAGS = @TEST_LDFLAGS@
+haproxy_protocol_testdriver_SOURCES = test_haproxy_protocol.c mock_msg.c mock_msg.h \
+	mock_get_random.c \
+	$(top_srcdir)/src/openvpn/platform.c \
+	$(top_srcdir)/src/openvpn/buffer.c \
+	$(top_srcdir)/src/openvpn/haproxy_protocol.c
+
 ssl_testdriver_CFLAGS  = \
 	-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
 	@TEST_CFLAGS@
tests/unit_tests/openvpn/test_haproxy_protocol.c new file +354 -0

354 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 4
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#8 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos, ralf_lici.

flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 4: Code-Review-1

(1 comment)

Patchset:

PS4:
Needs uncrustify fix
Needs rebase to fix issue with t_server_null tests

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 4
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: ralf_lici <[email protected]...>
Gerrit-Comment-Date: Wed, 08 Jan 2025 11:51:22 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#9 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos, ralf_lici.

Hello flichtenheld, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/685?usp=email

to look at the new patch set (#5).

The following approvals got outdated and were removed:
Code-Review-1 by flichtenheld

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Add support for HAProxy's PROXY protocol

The PROXY protocol is a simple protocol that can be used to communicate connection information such as a client's address across multiple layers of NAT or TCP proxies. This commit adds support for the parsing of the v1 and v2 PROXY protocol header but does not yet parse the TLV data in the v2 header.

Check if a PROXY protocol header was sent by the client at the beginning of the connection. If so, parse it, store the extracted informations and replace the proxy address with the real client address. Checking for the header is done only if the packet has an invalid length so that there's minimal overhead for the common case.

Also add a small document file showing how to configure HAProxy to test the feature and a unit test for the parsing.

Change-Id I9766c68feaba71279f26c3310eee4b5ca215e6ac

---
M CMakeLists.txt
A doc/haproxy-protocol.txt
M src/openvpn/Makefile.am
A src/openvpn/haproxy_protocol.c
A src/openvpn/haproxy_protocol.h
M src/openvpn/init.c
M src/openvpn/multi.c
M src/openvpn/multi.h
M src/openvpn/openvpn.h
M src/openvpn/socket.c
M src/openvpn/socket.h
M tests/unit_tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/test_haproxy_protocol.c

13 files changed, 1,333 insertions(+), 13 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/85/685/5

CMakeLists.txt +2 -0
index 9ffcc89..01c9e2b 100644
@@ -422,6 +422,8 @@
     src/openvpn/fragment.h
     src/openvpn/gremlin.c
     src/openvpn/gremlin.h
+    src/openvpn/haproxy_protocol.c
+    src/openvpn/haproxy_protocol.h
     src/openvpn/helper.c
     src/openvpn/helper.h
     src/openvpn/httpdigest.c
doc/haproxy-protocol.txt new file +166 -0

166 lines not on this page · raw message

src/openvpn/Makefile.am +1 -0
index d6d6592..f2e12d6 100644
@@ -68,6 +68,7 @@
 	forward.c forward.h \
 	fragment.c fragment.h \
 	gremlin.c gremlin.h \
+	haproxy_protocol.c haproxy_protocol.h \
 	helper.c helper.h \
 	httpdigest.c httpdigest.h \
 	lladdr.c lladdr.h \
src/openvpn/haproxy_protocol.c new file +487 -0

487 lines not on this page · raw message

src/openvpn/haproxy_protocol.h new file +184 -0

184 lines not on this page · raw message

src/openvpn/init.c +9 -0
index 2bd5518..af9c5c6 100644
@@ -55,6 +55,7 @@
 #include "mudp.h"
 #include "dco.h"
 #include "tun_afunix.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -4074,6 +4075,14 @@
             c->c1.link_socket_addrs[i].bind_local = NULL;
         }
     }
+
+    /* Clear and free the memory allocated for the HAProxy protocol obj */
+    if (c->c1.haproxy_protocol)
+    {
+        haproxy_protocol_reset(c->c1.haproxy_protocol);
+        free(c->c1.haproxy_protocol);
+        c->c1.haproxy_protocol = NULL;
+    }
 }
 
 /*
src/openvpn/multi.c +73 -2

118 lines not on this page · raw message

src/openvpn/multi.h +9 -0
index 9b6834a..76e265e 100644
@@ -293,6 +293,15 @@
 void multi_process_float(struct multi_context *m, struct multi_instance *mi,
                          struct link_socket *ls);
 
+/**
+ * Replaces the proxy address with the real client address obtained via the
+ * HAProxy PROXY protocol.
+ *
+ * If the real address is already in use, the new client will take precedence.
+ */
+void
+multi_replace_proxy_addr(struct multi_context *m, struct multi_instance *mi);
+
 #define MPP_PRE_SELECT             (1<<0)
 #define MPP_CONDITIONAL_PRE_SELECT (1<<1)
 #define MPP_CLOSE_ON_SIGNAL        (1<<2)
src/openvpn/openvpn.h +3 -0
index df43bba..08775fe 100644
@@ -45,6 +45,7 @@
 #include "pool.h"
 #include "plugin.h"
 #include "manage.h"
+#include "haproxy_protocol.h"
 
 /*
  * Our global key schedules, packaged thusly
@@ -192,6 +193,8 @@
     struct socks_proxy_info *socks_proxy;
     bool socks_proxy_owned;
 
+    struct haproxy_protocol_info *haproxy_protocol;
+
     /* persist --ifconfig-pool db to file */
     struct ifconfig_pool_persist *ifconfig_pool_persist;
     bool ifconfig_pool_persist_owned;
src/openvpn/socket.c +29 -9
index f9f2a3b..370ffb9 100644
@@ -39,6 +39,7 @@
 #include "manage.h"
 #include "openvpn.h"
 #include "forward.h"
+#include "haproxy_protocol.h"
 
 #include "memdbg.h"
 
@@ -1644,7 +1645,7 @@
 stream_buf_close(struct stream_buf *sb);
 
 static bool
-stream_buf_added(struct stream_buf *sb, int length_added);
+stream_buf_added(struct stream_buf *sb, int length_added, bool haproxy_protocol_allowed);
 
 /* For stream protocols, allocate a buffer to build up packet.
  * Called after frame has been finalized. */
@@ -2662,7 +2663,7 @@
     {
         ASSERT(buf_copy(&sock->stream_buf.buf, &sock->stream_buf.residual));
         ASSERT(buf_init(&sock->stream_buf.residual, 0));
-        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0);
+        sock->stream_buf.residual_fully_formed = stream_buf_added(&sock->stream_buf, 0, false);
         dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=%d",
              sock->stream_buf.residual_fully_formed ? "YES" : "NO",
              sock->stream_buf.residual.len);
@@ -2677,7 +2678,8 @@
 
 static bool
 stream_buf_added(struct stream_buf *sb,
-                 int length_added)
+                 int length_added,
+                 const bool haproxy_protocol_allowed)
 {
     dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=%d", length_added);
     if (length_added > 0)
@@ -2694,7 +2696,9 @@
 #if PORT_SHARE
         if (sb->port_share_state == PS_ENABLED)
         {
-            if (!is_openvpn_protocol(&sb->buf))
+            if (!is_openvpn_protocol(&sb->buf)
+                && (!haproxy_protocol_allowed
+                    || haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf)) == HAPROXY_PROTOCOL_VERSION_INVALID))
             {
                 msg(D_STREAM_ERRORS, "Non-OpenVPN client protocol detected");
                 sb->port_share_state = PS_FOREIGN;
@@ -2713,10 +2717,26 @@
 
         if (sb->len < 1 || sb->len > sb->maxlen)
         {
-            msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
-            stream_buf_reset(sb);
-            sb->error = true;
-            return false;
+            haproxy_protocol_version_t hp_version = HAPROXY_PROTOCOL_VERSION_UNKNOWN;
+            int hp_len = -1;
+            /* if it's allowed (first packet), check for a HAProxy protocol header */
+            if (haproxy_protocol_allowed)
+            {
+                /* undo the reading of net_size */
+                ASSERT(buf_prepend(&sb->buf, sizeof(net_size)));
+                hp_version = haproxy_protocol_version(BPTR(&sb->buf), BLEN(&sb->buf));
+                hp_len = haproxy_protocol_header_len(BPTR(&sb->buf), BLEN(&sb->buf), hp_version);
+            }
+
+            /* we do not allow partial headers */
+            if (hp_version <= 0 || hp_len < 0 || hp_len > sb->buf.len)
+            {
+                msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
+                stream_buf_reset(sb);
+                sb->error = true;
+                return false;
+            }
+            sb->len = hp_len;
         }
     }
 
@@ -3300,7 +3320,7 @@
     }
 
     if (sock->stream_buf.residual_fully_formed
-        || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */
+        || stream_buf_added(&sock->stream_buf, len, sock->haproxy_protocol_allowed)) /* packet complete? */
     {
         stream_buf_get_final(&sock->stream_buf, buf);
         stream_buf_reset(&sock->stream_buf);
src/openvpn/socket.h +3 -0
index 16106d4..db28f7f 100644
@@ -244,6 +244,9 @@
     const char *proxy_dest_host;
     const char *proxy_dest_port;
 
+    /* check HAProxy PROXY protocol only if true */
+    bool haproxy_protocol_allowed;
+
     /* Pointer to the server-poll to trigger the timeout in function which have
      * their own loop instead of using the main oop */
     struct event_timeout *server_poll_timeout;
tests/unit_tests/openvpn/Makefile.am +13 -2
index 307f9ed..cb22f08 100644
@@ -10,8 +10,9 @@
 test_binaries += argv_testdriver buffer_testdriver
 endif
 
-test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \
-	pkt_testdriver ssl_testdriver user_pass_testdriver
+test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver \
+				 haproxy_protocol_testdriver ncp_testdriver misc_testdriver \
+				 pkt_testdriver ssl_testdriver user_pass_testdriver
 
 if HAVE_LD_WRAP_SUPPORT
 if !WIN32
@@ -74,6 +75,16 @@
 	$(top_srcdir)/src/openvpn/win32-util.c \
 	$(top_srcdir)/src/openvpn/mss.c
 
+haproxy_protocol_testdriver_CFLAGS  = \
+	-I$(top_srcdir)/include -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/compat \
+	@TEST_CFLAGS@
+haproxy_protocol_testdriver_LDFLAGS = @TEST_LDFLAGS@
+haproxy_protocol_testdriver_SOURCES = test_haproxy_protocol.c mock_msg.c mock_msg.h \
+	mock_get_random.c \
+	$(top_srcdir)/src/openvpn/platform.c \
+	$(top_srcdir)/src/openvpn/buffer.c \
+	$(top_srcdir)/src/openvpn/haproxy_protocol.c
+
 ssl_testdriver_CFLAGS  = \
 	-I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
 	@TEST_CFLAGS@
tests/unit_tests/openvpn/test_haproxy_protocol.c new file +354 -0

354 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 5
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: ralf_lici <[email protected]...>
Gerrit-MessageType: newpatchset

#10 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

ralf_lici has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/685?usp=email )

Change subject: Add support for HAProxy's PROXY protocol
......................................................................

Patch Set 5:

(4 comments)

Patchset:

PS4:

Quoted 1 line

Needs uncrustify fix […]

Done

File doc/proxy-protocol.txt:

http://gerrit.openvpn.net/c/openvpn/+/685/comment/3fa8c036_061fb696 :
PS1, Line 11: a binary version (v2).

Quoted 1 line

It also supports UDP but we only handle the TCP case so I will state it more clearly in the document […]

Done

http://gerrit.openvpn.net/c/openvpn/+/685/comment/bdb719ac_ff4981c5 :
PS1, Line 108:

Quoted 1 line

Basically LOCAL commands are used when the proxy sends health-checks to the server and therefore I g […]

Done

http://gerrit.openvpn.net/c/openvpn/+/685/comment/1c49ef62_150309e4 :
PS1, Line 115: DGRAM (0x2).

Quoted 1 line

The PROXY protocol works both with TCP and with UDP but we support only TCP. […]

Done

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/685?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9766c68feaba71279f26c3310eee4b5ca215e6ac
Gerrit-Change-Number: 685
Gerrit-PatchSet: 5
Gerrit-Owner: ralf_lici <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 09 Jan 2025 14:46:35 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: plaisthos <[email protected]...>
Comment-In-Reply-To: flichtenheld <[email protected]...>
Comment-In-Reply-To: ralf_lici <[email protected]...>
Gerrit-MessageType: comment